package jianzhi;

/**
 * description:
 * author:zt
 * date:2021-10-28
 */

/**
 * 输入一个英文句子，翻转句子中单词的顺序，但单词内字符的顺序不变。为简单起见，标点符号和普通字母一样处理。例如输入字符串"I am a student. "，则输出"student. a am I"。
 */
public class jianzhi58 {
    public String reverseWords(String s) {
        s.trim();
        int len = s.length();
        int left = len-1, right = len-1;
        StringBuilder sb = new StringBuilder();
        while (left >= 0){
            //找到一个单词
            while (left >= 0 && s.charAt(left) != ' ') left--;
            sb.append(s.substring(left+1,right+1)+" ");

            //除去空格，找下一个单词
            while (left >= 0 && s.charAt(left) == ' ') left--;
            right = left;
        }
        return sb.toString().trim();
    }
}
